package com.decent.service.impl;

import com.decent.dao.OrderMapper;
import com.decent.entity.Order;
import com.decent.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 根据订单号找获得订单数量
     *
     * @param orderNo 订单号
     * @return 订单数量
     */
    @Override
    public Long getOrderNoCount(String orderNo) {
        Long orderNoCount = orderMapper.getOrderNoCount(orderNo);
        System.out.println(orderNoCount);
        return orderNoCount;
    }

    /**
     * 插入订单
     *
     * @param order 订单实体
     * @return 影响行数
     */
    @Override
    @Transactional
    public Integer insertOrder(Order order) {
        return orderMapper.insert(order);
    }

    /**
     * 门店绑定订单
     *
     * @param storeId 门店id
     * @param orderNo 订单号
     * @return 影响行数
     */
    @Override
    @Transactional
    public Integer bindStore(Integer storeId, String orderNo) {
        return orderMapper.bindStore(storeId, orderNo);
    }

    /**
     * 订单分页
     *
     * @param pageNum   当前页数
     * @param pageSize  每页条数
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @Override
    public PageInfo<Order> getPageOrder(Integer pageNum, Integer pageSize, String startTime, String endTime) {
        return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
            orderMapper.getPageOrder(startTime, endTime);
        });
    }

    /**
     * 订单信息查询
     *
     * @param orderNo 订单号
     * @return List{orderPrice,orderNo,storeName}
     */
    @Override
    public Order getOrderInfo(String orderNo) {
        return orderMapper.getOrderInfo(orderNo);
    }
}
